using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Hamastar.SimMAGIC.Designer
{
	public class PaintService
	{
		private Canvas canvas;

		private Shape drawingShape;

		private bool capture;

		private Point pDown;

		private PathFigure pathFigure;

		[CompilerGenerated]
		private DrawType _003CDrawType_003Ek__BackingField;

		[CompilerGenerated]
		private Rect _003CShapeRect_003Ek__BackingField;

		[CompilerGenerated]
		private string _003CShapeName_003Ek__BackingField;

		public DrawType DrawType
		{
			[CompilerGenerated]
			get
			{
				return _003CDrawType_003Ek__BackingField;
			}
			[CompilerGenerated]
			set
			{
				_003CDrawType_003Ek__BackingField = value;
			}
		}

		public Rect ShapeRect
		{
			[CompilerGenerated]
			get
			{
				return _003CShapeRect_003Ek__BackingField;
			}
			[CompilerGenerated]
			set
			{
				_003CShapeRect_003Ek__BackingField = value;
			}
		}

		public string ShapeName
		{
			[CompilerGenerated]
			get
			{
				return _003CShapeName_003Ek__BackingField;
			}
			[CompilerGenerated]
			set
			{
				_003CShapeName_003Ek__BackingField = value;
			}
		}

		public PaintService(Canvas cc)
		{
			canvas = cc;
		}

		public void MouseDown(Point pp)
		{
			capture = true;
			pDown = pp;
			if (DrawType == DrawType.Brush)
			{
				pathFigure = new PathFigure();
				pathFigure.StartPoint = pDown;
				pathFigure.IsClosed = false;
				LineSegment lineSegment = new LineSegment();
				lineSegment.Point = pDown;
				pathFigure.Segments.Add(lineSegment);
				PathGeometry pathGeometry = new PathGeometry();
				pathGeometry.Figures.Add(pathFigure);
				drawingShape = new Path();
				(drawingShape as Path).Data = pathGeometry;
				DrawCapture();
			}
			else if (DrawType == DrawType.Ellipse)
			{
				drawingShape = new Ellipse();
				DrawCapture();
			}
			else if (DrawType == DrawType.Rectangle)
			{
				drawingShape = new Rectangle();
				DrawCapture();
			}
		}

		public void MouseMove(Point pMove)
		{
			if (!capture)
			{
				return;
			}
			if (DrawType == DrawType.Brush)
			{
				LineSegment lineSegment = Enumerable.LastOrDefault(pathFigure.Segments) as LineSegment;
				if (lineSegment != null)
				{
					double num = 10.0;
					if ((lineSegment.Point - pMove).Length > num)
					{
						LineSegment lineSegment2 = new LineSegment();
						lineSegment2.Point = pMove;
						pathFigure.Segments.Add(lineSegment2);
					}
				}
			}
			else if (DrawType == DrawType.Ellipse || DrawType == DrawType.Rectangle)
			{
				if (pMove.X <= pDown.X && pMove.Y <= pDown.Y)
				{
					drawingShape.Margin = new Thickness(pMove.X, pMove.Y, 0.0, 0.0);
				}
				else if (pMove.X >= pDown.X && pMove.Y <= pDown.Y)
				{
					drawingShape.Margin = new Thickness(pDown.X, pMove.Y, 0.0, 0.0);
				}
				else if (pMove.X >= pDown.X && pMove.Y >= pDown.Y)
				{
					drawingShape.Margin = new Thickness(pDown.X, pDown.Y, 0.0, 0.0);
				}
				else if (pMove.X <= pDown.X && pMove.Y >= pDown.Y)
				{
					drawingShape.Margin = new Thickness(pMove.X, pDown.Y, 0.0, 0.0);
				}
				drawingShape.Width = Math.Abs(pMove.X - pDown.X);
				drawingShape.Height = Math.Abs(pMove.Y - pDown.Y);
			}
		}

		public Shape MouseUp(Point pUp)
		{
			Shape result = null;
			if (drawingShape != null)
			{
				if (DrawType == DrawType.Brush)
				{
					ShapeRect = (drawingShape as Path).Data.Bounds;
				}
				else if (DrawType == DrawType.Ellipse || DrawType == DrawType.Rectangle)
				{
					ShapeRect = new Rect(drawingShape.Margin.Left, drawingShape.Margin.Top, drawingShape.Width, drawingShape.Height);
				}
				else if (DrawType == DrawType.Line || DrawType == DrawType.Arrow)
				{
					ShapeRect = new Rect(pDown, pUp);
					if (ShapeRect.Height < 10.0)
					{
						ShapeRect = new Rect(ShapeRect.X, ShapeRect.Y, ShapeRect.Width, 10.0);
					}
					else if (ShapeRect.Width < 10.0)
					{
						ShapeRect = new Rect(ShapeRect.X, ShapeRect.Y, 10.0, ShapeRect.Height);
					}
				}
				drawingShape.Name = ShapeName;
				SetDrawingShape();
				result = drawingShape;
			}
			capture = false;
			drawingShape = null;
			DrawType = DrawType.None;
			return result;
		}

		private void DrawCapture()
		{
			double[] collection = new double[2]
			{
				3.0,
				1.0
			};
			drawingShape.StrokeDashArray = new DoubleCollection(collection);
			drawingShape.StrokeThickness = 3.0;
			drawingShape.Stroke = Brushes.Black;
			canvas.Children.Add(drawingShape);
			Panel.SetZIndex(drawingShape, canvas.Children.Count + 1);
		}

		private void SetDrawingShape()
		{
			drawingShape.StrokeDashArray = null;
			drawingShape.StrokeThickness = 3.0;
			drawingShape.Stroke = Brushes.Black;
			drawingShape.Stretch = Stretch.Fill;
			drawingShape.Margin = new Thickness(0.0, 0.0, 0.0, 0.0);
			drawingShape.Width = double.NaN;
			drawingShape.Height = double.NaN;
			drawingShape.IsHitTestVisible = false;
			canvas.Children.Remove(drawingShape);
		}
	}
}
